/**
 * The MIT License (MIT)
 * 
 * Copyright (c) 2016 mo_yq5 (mo_yq5@163.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 */
package com.vulgarsdk.captcha;

/**
 * 验证码工具类
 * @author mo_yq5
 * @since 2016年1月17日
 */
public class CaptchaUtils {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
	
	/**
	 * 生成数字验证码
	 * @since 2016年1月17日
	 * @param length 验证码长度
	 * @return
	 */
	public static String mkDigitCaptcha(int length) {
		return String.valueOf(Math.round(Math.random() * (Math.pow(10, length + 1) - Math.pow(10, length) - 1) + Math.pow(10, length)));
	}
	
	/**
	 * 生成由字母组成的验证码
	 * @since 2016年1月17日
	 * @param length
	 * @return
	 */
	public static String mkLetterCaptcha(int length) {
		final int bound = hexDigits.length - 1;
		String str = "";
		for (int i = 0; i < length; i++) {
			int r = (int)Math.round(Math.random() * (bound-10) + 10);
			str += hexDigits[r];
		}
		return str;
	}
	
	/**
	 * 生成数字和字母组成的验证码
	 * @since 2016年1月17日
	 * @param length 验证码长度
	 * @return
	 */
	public static String mkMixtureCaptcha(int length) {
		final int bound = hexDigits.length - 1;
		String str = "";
		for (int i = 0; i < length; i++) {
			int r = (int)Math.round(Math.random() * bound);
			str += hexDigits[r];
		}
		return str;
	}
	
}
